package com.cw.jeeyt.controller.mdata;

import javax.annotation.Resource;

import java.util.ArrayList;

import org.springframework.stereotype.Controller;

import com.cw.jeeyt.api.dto.response.JsonResultDto;

import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.cw.jeeyt.common.DataTable;
import com.cw.jeeyt.common.ResultData;
import com.cw.jeeyt.common.enums.EnumOperateFlag;
import com.cw.jeeyt.common.web.BaseController;

import com.cw.jeeyt.service.bo.mdata.MdataBanksBo;
import com.cw.jeeyt.service.intf.mdata.MdataBanksService;
import com.cw.jeeyt.service.directive.PageDictDirective;
import com.cw.jeeyt.service.directive.PermissionDirective;
import com.cw.lang.common.utils.PageUtil;
import com.cw.lang.common.utils.CollectionUtil;
import com.cw.lang.mybatis.dto.PageInfo;
import com.cw.lang.mybatis.dto.PageParam;

import lombok.extern.slf4j.Slf4j;

/**
 * 银行控制层
 *
 * @author admin
 * @date 2021-03-26 22:30:39
 */
@Controller
@RequestMapping("/mdataBanks")
@Slf4j
public class MdataBanksController extends BaseController {
    @Resource
    private MdataBanksService mdataBanksService;

    /**
     * 跳转到银行列表查询页面
     *
     * @return
     */
    @RequestMapping(value = "/query")
    public ModelAndView query() {

        ModelAndView mv = new ModelAndView("mdata/mdataBanks_index");
        mv.addObject("permission", new PermissionDirective());
        mv.addObject("PageDict", new PageDictDirective());
        return mv;
    }

    /**
     * 加载DataTable列表数据
     *
     * @param dataTable datatable数据
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/listMdataBanks")
    public ResultData listMdataBanks(@RequestBody DataTable dataTable) {

        MdataBanksBo mdataBanksBo = JSONObject.parseObject(JSON.toJSONString(dataTable.queryCond), MdataBanksBo.class);
        if (mdataBanksBo == null) {
            mdataBanksBo = new MdataBanksBo();
        }

        PageParam pageParam = new PageParam();
        pageParam.setPageNum(PageUtil.calPageNum(dataTable.getStart(), dataTable.getLength()));
        pageParam.setPageSize(dataTable.getLength());
        PageInfo<MdataBanksBo> pageInfo = mdataBanksService.selectPage(mdataBanksBo, pageParam);
        return ResultData.builder()
            .draw(dataTable.getDraw())
            .data(CollectionUtil.isNotEmpty(pageInfo.getList()) ? pageInfo.getList() : new ArrayList())
            .recordsTotal(pageInfo.getTotal())
            .recordsFiltered(pageInfo.getTotal())
            .build();
    }

    /**
     * 跳转到银行表单页面
     *
     * @param operation 操作  add 新增 edit修改 view查看
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/to-form")
    public ModelAndView editEdit(String bankId, String operation) {

        ModelAndView mv = new ModelAndView("mdata/mdataBanks_form");
        MdataBanksBo mdataBanksBo = null;
        if (EnumOperateFlag.ADD.getCode()
            .equals(operation)) {
            mdataBanksBo = new MdataBanksBo();
        } else {
            mdataBanksBo = mdataBanksService.selectByKey(bankId);
        }
        mv.addObject("mdataBanks", mdataBanksBo);
        mv.addObject("operation", operation);
        mv.addObject("PageDict", new PageDictDirective());
        mv.addObject("permission", new PermissionDirective());
        return mv;
    }

    /**
     * 根据操作类型保存银行数据
     *
     * @param mdataBanksBo
     * @param operation add 新增  edit 修改
     */
    @ResponseBody
    @RequestMapping(value = "/saveMdataBanks", method = RequestMethod.POST)
    public JsonResultDto saveMdataBanks(MdataBanksBo mdataBanksBo, String operation) {

        JsonResultDto jsonResult = new JsonResultDto(false);
        try {
            if (operation.equals(EnumOperateFlag.ADD.getCode())) {
                mdataBanksService.insert(mdataBanksBo);
            } else if (operation.equals(EnumOperateFlag.EDIT.getCode())) {
                mdataBanksService.modify(mdataBanksBo);
            }
            jsonResult.setMsg("保存成功");
            jsonResult.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setMsg("保存失败:" + e.getMessage());
        }
        return jsonResult;
    }

    /**
     * 删除银行
     *
     * @param mdataBanksBo
     * @throws Exception
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public JsonResultDto delete(MdataBanksBo mdataBanksBo) throws Exception {

        JsonResultDto jsonResult = new JsonResultDto(false);
        try {
            mdataBanksService.delete(mdataBanksBo);
            jsonResult.setMsg("删除成功");
            jsonResult.setSuccess(true);
        } catch (Exception e) {
            e.printStackTrace();
            jsonResult.setMsg("删除失败:" + e.getMessage());
        }
        return jsonResult;
    }
}
